package com.int_tech.utils.poi;

import com.int_tech.station.entity.Station;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/23.
 */
public class ReadExcelStation {
    public static List<Station> readExcel(MultipartFile file) {
        List<Station> list = new ArrayList<>();
        HSSFWorkbook workbook = null;
        DecimalFormat decimalFormat = new DecimalFormat("0");
        try {
            // 读取Excel文件
            InputStream inputStream = file.getInputStream();
            workbook = new HSSFWorkbook(inputStream);
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 循环工作表
        for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
            HSSFSheet sheet = workbook.getSheetAt(numSheet);
            if (sheet == null) {
                continue;
            }
            // 循环行
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                HSSFRow row = sheet.getRow(rowNum);
                if (row == null) {
                    continue;
                }

                // 将单元格中的内容存入集合
                Station station = new Station();

                HSSFCell cell = row.getCell(0);
                if (cell == null) {
                    continue;
                }
                station.setStationCode(Integer.valueOf(decimalFormat.format(cell.getNumericCellValue())));

                cell = row.getCell(1);
                if (cell == null) {
                    continue;
                }
                station.setName(cell.getStringCellValue());

                cell = row.getCell(2);
                if (cell == null) {
                    continue;
                }
                station.setClentName(cell.getStringCellValue());

                String area = "";

                cell = row.getCell(3);
                if (cell == null) {
                    continue;
                }
                String areaA = cell.getStringCellValue();

                cell = row.getCell(4);
                if (cell == null) {
                    continue;
                }
                String areaB = cell.getStringCellValue();

                cell = row.getCell(5);
                if (cell == null) {
                    continue;
                }
                String areaC = cell.getStringCellValue();

                cell = row.getCell(6);
                if (cell == null) {
                    continue;
                }
                String areaD = cell.getStringCellValue();

                area = areaA+"&"+areaB+"&"+areaC+"&"+areaD;

                station.setArea(area);

                list.add(station);
            }
        }
        return list;
    }
}
